package com.systop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.systop.entity.Banner;
import com.systop.service.BannerService;
import com.systop.service.OSSFileManage;
import com.systop.utils.FileUploadResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private OSSFileManage ossFileManage;

    @RequestMapping(value = "/findAllImg",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findAllImg(Banner banner){
        Map<String,Object> map = new HashMap<>();
        //查询符合条件图片数量
        int pageCount = bannerService.countImg(banner);
        //分页计算
        banner.setStart((banner.getStart()-1)*banner.getPageSize());
        //查询所有符合条件图片信息
        List list = bannerService.findAllImg(banner);
        //查询所有符合条件图片
        List imgList = bannerService.findAllImg1(banner);
        //处理当前页面空值
        if(CollectionUtils.isEmpty(list) && banner.getStart()>0){
            //设置当本页没有数据时返回上一页
            banner.setStart(banner.getStart()/banner.getPageSize());
            //设置当本页没有数据时返回第一页
            // banner.setStart(1);
            return findAllImg(banner);
        }
        //设置返回值
        map.put("bannerList", list);
        map.put("bannerImgList",imgList);
        map.put("pageCount",pageCount);
        return map;
    };

    //前台查询banner
    @RequestMapping(value = "/findAllRotation",method = RequestMethod.GET)
    @ResponseBody

    public Map<String,Object> findAllRotation(){
        Map<String,Object> map = new HashMap<>();
        Banner banner = new Banner();
        //查询所有符合条件图片
        List imgList = bannerService.findAllImg1(banner);
        map.put("bannerImgList",imgList);
        return map;
    }

    //添加banner数据
    @RequestMapping(value = "/addImg",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addImg(@RequestParam("file") MultipartFile file,
                                     @RequestParam("banner") String banner){
        System.out.println(banner);
        Map<String,Object> map = new HashMap<>();
        int i= 0;
        //数据格式转换
        Banner bannerPojo= JSON.parseObject(JSON.parse(JSON.toJSONString(banner)).toString(),
                new TypeReference<Banner>(){});
        System.out.println(bannerPojo);
        //上传图片到OSS服务器上
        FileUploadResult result = ossFileManage.bannerUpload(file);
        if(result.getStatus()=="done"){
            //获取上传完毕后的相应图片路径
            bannerPojo.setBannerImg(result.getName());
            i = bannerService.addImg(bannerPojo);
        }else if(result.getStatus()=="error"){
            map.put("msg",result.getStatus()+",上传失败,数据错误");
            return map;
        }
        map.put("addSuccNum",i);
        if(i>0){
            map.put("msg", "添加成功!");
        }else{
            map.put("msg","添加失败,数据错误");
            return map;
        }
        return map;
    }

    //根据id删除数据
    @RequestMapping(value = "/delBanner",method = RequestMethod.DELETE)
    @ResponseBody
    public Map<String,Object> delBanner(Banner banner){
        //定义键值对返回数据
        Map<String,Object> map = new HashMap<String,Object>();
        int i = 0;

        if(banner.getBannerImg() !=null || banner.getBannerImg().length()>0){
            String url = banner.getBannerImg();
            System.out.println(url);

            url = url.replace("https://oss-container.oss-cn-beijing.aliyuncs.com/","");
            System.out.println(url);
            System.out.println(url.indexOf("?"));
            //判断路径中是否存在?
            // if(url.indexOf("?")!=-1){
            //     url = url.substring(0,url.indexOf("?"));
            //     System.out.println(url);
                FileUploadResult result = ossFileManage.delete(url);
            // }
        }
        i = bannerService.delBanner(banner);
        map.put("delSuccNum",i);
        if(i>0){
            map.put("msg", "删除成功!");
        }else{
            map.put("msg","删除失败,用户不存在");
        }
        return map;
    }

    //更新数据方法
    @RequestMapping(value = "/udpBanner",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> udpBanner(@RequestBody Banner banner){
        System.out.println(banner);
        Map<String,Object> map = new HashMap<>();
        int i  = bannerService.udpBanner(banner);
        map.put("udpSuccNum",i);
        if(i>0){
            map.put("msg", "修改成功!");
        }else{
            map.put("msg","数据不存在");
        }
        return map;
    }
}
